package com.example.mykotilin.room.viewmodel

import androidx.lifecycle.ViewModel
import com.example.mykotilin.room.persistence.User
import com.example.mykotilin.room.persistence.UserDao
import io.reactivex.Completable
import io.reactivex.Flowable

/**
 * 创建view model
 */
class UserViewModel(private val dataSource:UserDao) : ViewModel(){
    companion object{
        const val USER_ID = "1"
    }
    /**
     * 获取用户名称，利用UserDao去处理
     */
    fun userName() : Flowable<String>{
        return  dataSource.getUserById(USER_ID).map {
            user->user.userName
        }
    }
    /**
     * 插入用户名
     */
    fun  updateUserName(userName:String):Completable{
        val  user = User(USER_ID,userName)
        return dataSource.insertUser(user);
    }
}